package cn.sccdlg.ssm.service.impl;

import cn.sccdlg.ssm.dao.IRoleDao;
import cn.sccdlg.ssm.domain.Permission;
import cn.sccdlg.ssm.domain.Role;
import cn.sccdlg.ssm.service.RoleService;
import cn.sccdlg.ssm.utils.UuidUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author RaoShuang
 * @emil 991207823@qq.com
 * @date 2020/10/28 22:23
 * 角色业务层实现类
 */
@Service("/roleService")
@Transactional(rollbackFor = {Exception.class, Error.class})
public class RoleServiceImpl implements RoleService {

    @Autowired
    private IRoleDao roleDao;

    @Override
    public List<Role> findAll() throws Exception {
        return roleDao.findAll();
    }

    @Override
    public void save(Role role) throws Exception {
        role.setId(UuidUtils.getUnid());
        roleDao.save(role);
    }

    @Override
    public Role findById(String roleId) throws Exception {
        return roleDao.findById(roleId);
    }

    @Override
    public List<Permission> findOtherPermissions(String roleId) throws Exception {
        return roleDao.findOtherPermissions(roleId);
    }

    @Override
    public void addPermissionToRole(String roleId, String[] permissionIds) throws Exception {
        for(String permissionId:permissionIds){
            roleDao.addPermissionToRole(roleId, permissionId);
        }
    }

    @Override
    public void deleteRoleById(String roleId) throws Exception {
        //从user_role表中删除
        roleDao.deleteFromUser2RoleByRoleId(roleId);
        //从role_permission表中删除
        roleDao.deleteFromRole2PermissionByRoleId(roleId);
        //从role表中删除
        roleDao.deleteRoleById(roleId);
    }
}
